package com.mj.BinaryTreebak.BinarySearchTree;

import com.mj.printer.BinaryTrees;
import com.mj.printer.Files;

import java.util.Comparator;

/**
 * @Author: LDeng
 * @Date: 2021-02-26 15:58
 */
public class Main {

    private static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test1() {
        Integer data[] = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.print(bst);
    }

    static void test2() {
        Integer data[] = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        BinarySearchTree<Person> bst1 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        for (int i = 0; i < data.length; i++) {
            bst1.add(new Person(data[i]));
        }
        BinaryTrees.print(bst1);
    }


    static void test3() {
        BinarySearchTree<Integer> bst3 = new BinarySearchTree<>();
        for (int i = 0; i < 100; i++) {
            bst3.add((int) (Math.random() * 100));
        }
        BinaryTrees.print(bst3);
        String str = BinaryTrees.printString(bst3);
        str += "\n";
        Files.writeToFile("c:/out/1.txt", str, true);
    }

    static void test6() {
        Integer data[] = new Integer[]{7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.print(bst);
        System.out.println("\n ==============前序遍历==============");
        //通过接口传递处理逻辑
        bst.preorderTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_ ");
                return element == 2 ? true : false;//遍历到2就停止
            }
        });
        System.out.println("\n ==============中序遍历==============");
        bst.inorderTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_ ");
                return element == 4 ? true : false;//遍历到4玖停止
            }
        });
        System.out.println("\n ==============后序遍历==============");
        bst.postorderTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_ ");
                return element == 4 ? true : false;//遍历到9就停止
            }
        });
        //层序遍历接口,可以实现对元素传递出来自定义处理
        System.out.println("\n ==============层序遍历接口==============");
        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_ ");
                return element == 9 ? true : false;
            }
        });

        System.out.println("\n ==============前需遍历应用,打印树==============");
        System.out.println(bst);


        System.out.println("\n ==============遍历应用,二叉树高度(递归)==============");
        System.out.println("递归计算二叉树高度为： "+bst.height());

        System.out.println("\n ==============遍历应用,二叉树高度(迭代)==============");
        System.out.println("递归计算二叉树高度为： "+bst.height1());
    }

    static void test9() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 10; i++) {
            bst.add((int) (Math.random() * 100));
        }

        BinarySearchTree<Integer> bst1 = new BinarySearchTree<>();
        Integer data[] = new Integer[]{7, 4, 9, 2, 5};
        for (int i = 0; i < data.length; i++) {
            bst1.add(data[i]);
        }

        BinaryTrees.print(bst1);
        System.out.println("\n"+"二叉树高度计算："+bst1.height1());
        System.out.println("是否是完全二叉树："+bst1.isComplete());

    }

    static void test10(){
        Integer data[] = new Integer[]{7,4,9,2,5,8,11,3,12,1};
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.print(bst);
        System.out.println("\n");
        bst.remove1(7);
        BinaryTrees.print(bst);
    }

    public static void main(String[] args) {

        //test1();
        //test2();
        //test3();
        //test6();
       // test9();
        test10();
//        //传递compartor
//        BinarySearchTree<Person> bst2=new BinarySearchTree<>(new PersonComparator());
//        bst2.add(new Person(12));
//        bst2.add(new Person(15));
//        BinarySearchTree<Person> bst3=new BinarySearchTree<>(new PersonComparator2());
//        bst3.add(new Person(12));
//        bst3.add(new Person(15));
//        //不传compartor
//        BinarySearchTree<Person> bst4=new BinarySearchTree<>();
//        bst3.add(new Person(12));
//        bst3.add(new Person(15));
//
//        //匿名内部类
//        BinarySearchTree<Person> bst5=new BinarySearchTree<>(new Comparator<Person>() {
//            @Override
//            public int compare(Person o1, Person o2) {
//                return o1.getAge()-o2.getAge();
//            }
//        });
//        bst5.add(new Person(12));
//        bst5.add(new Person(15));
    }
}
